package handler

import (
	"2302a/weekly/api/request"
	"2302a/weekly/api/resp"
	"2302a/weekly/internal/dao"
	"2302a/weekly/internal/model"
	"github.com/gin-gonic/gin"
)

func AddGive(c *gin.Context) {
	var req request.AddGiveReq

	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 10000, "参数信息错误", err.Error())
		return
	}
	userId := c.GetUint("userId")
	var user model.User
	dao.GetOneById(userId, &user)
	if user.Id == 0 {
		resp.Error(c, 10000, "用户不存在", false)
		return
	}
	var video model.Video
	dao.GetOneById(req.VideoId, &video)
	if video.Id == 0 {
		resp.Error(c, 10000, "视频不存在", false)
		return
	}
	give := model.Give{
		UserId:  userId,
		VideoId: req.VideoId,
		Give:    req.Give,
		Status:  req.Status,
	}
	dao.Create(&give)
	resp.Success(c, "点赞成功", true)
}

func CancelGive(c *gin.Context) {
	var req request.CancelGiveReq

	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 10000, "参数信息错误", err.Error())
		return
	}
	userId := c.GetUint("userId")
	var user model.User
	dao.GetOneById(userId, &user)
	if user.Id == 0 {
		resp.Error(c, 10000, "用户不存在", false)
		return
	}
	var video model.Video
	dao.GetOneById(req.VideoId, &video)
	if video.Id == 0 {
		resp.Error(c, 10000, "视频不存在", false)
		return
	}
	dao.Update(&model.Give{Id: req.Id, VideoId: req.VideoId, Status: req.Status})
	resp.Success(c, "取消点赞成功", true)
}

func DetailsGive(c *gin.Context) {
	var req request.DetailsGiveReq

	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 10000, "参数信息错误", err.Error())
		return
	}
	userId := c.GetUint("userId")
	var user model.User
	dao.GetOneById(userId, &user)
	if user.Id == 0 {
		resp.Error(c, 10000, "用户不存在", false)
		return
	}
	var video model.Video
	dao.GetOneById(req.VideoId, &video)
	if video.Id == 0 {
		resp.Error(c, 10000, "视频不存在", false)
		return
	}
	var list []model.Give
	dao.GetListByFinds(&model.Give{
		Id:      req.Id,
		UserId:  userId,
		VideoId: req.VideoId,
	}, &list)
	resp.Success(c, "点赞状态与点赞数量成功", list)
}
